package com.lcp.se.controller;

import com.lcp.se.entity.data.Appointment;
import com.lcp.se.entity.result.PageResultForMini;
import com.lcp.se.entity.result.Result;
import com.lcp.se.entity.vo.AppointmentVO;
import com.lcp.se.service.AppointmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 预约消息处理
 *
 * @author lcp
 * @date 2020/7/27 15:46
 */
@RestController
@RequestMapping(path = "/user/appointment")
public class AppointmentController {

    @Resource
    private AppointmentService appointmentService;

    /**
     * 创建一个预约（求助端）
     * <p>
     *
     * @param appointment     预约内容
     * @param appointmentData 预约内容，用于发送给 se-im
     * @return 创建结果
     */
    @PostMapping(path = "/h/create")
    public Result<Object> create(AppointmentVO appointmentData, Appointment appointment) {
        return appointmentService.create(appointmentData, appointment);
    }

    /**
     * 查询未完成的预约（求助端）
     *
     * @param openId 求助者用户id
     * @return 查询结果
     */
    @PostMapping(path = "/h/uncompleted")
    public Result<AppointmentVO> unCompleted(@RequestHeader(name = "openId") String openId) {
        return appointmentService.unCompleted(openId);
    }

    /**
     * 取消预约（求助端）
     *
     * @param appointmentId 预约id
     * @return 结果
     */
    @PostMapping(path = "/h/cancel")
    public Result<Object> cancel(@RequestParam(name = "appointmentId") String appointmentId) {
        return appointmentService.complete(appointmentId,3);
    }

    /**
     * 未处理预约信息的分页查询（服务端）
     *
     * @param pageSize    当前页大小
     * @param currentPage 当前页
     * @return 查询结果
     */
    @PostMapping(path = "/v/list/untreated")
    public Result<PageResultForMini<AppointmentVO>> list(@RequestParam(name = "pageSize") Integer pageSize,
                                                         @RequestParam(name = "currentPage") Integer currentPage) {
        return appointmentService.detailList(pageSize, currentPage);
    }

    /**
     * 接受一个预约（服务端）
     *
     * @param appointmentId    预约 id
     * @param handleUserOpenId 接受该预约的志愿者id
     * @return 接受结果
     */
    @PostMapping(path = "/v/handle")
    public Result<Object> handle(@RequestParam(name = "appointmentId") String appointmentId,
                                 @RequestHeader(value = "openId") String handleUserOpenId) {
        return appointmentService.handle(appointmentId, handleUserOpenId);
    }

    /**
     * 结束/完成预约（求助端/服务端）
     *
     * @param appointmentId 预约id
     * @return 处理结果
     */
    @PostMapping(path = "/complete")
    public Result<Object> complete(@RequestParam(name = "appointmentId") String appointmentId) {
        return appointmentService.complete(appointmentId,2);
    }

    /**
     * 查询已接受未处理完（正在进行中）的预约（服务端）
     *
     * @param openId 志愿者 id
     * @return 查询结果
     */
    @PostMapping(path = "/v/processing")
    public Result<AppointmentVO> processingAppointment(@RequestParam(value = "userType") Integer userType,
                                                       @RequestHeader(name = "openId") String openId) {
        return appointmentService.processingAppointment(userType, openId);
    }
}